#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans,vis[500005],mp[4003][4003],tot,g[500005];
queue<int> q;
struct node{
	int x,y;
}a[500005];
int cmp(node x,node y){return x.x<y.x;}
int cmp2(node x,node y){return x.y<y.y;}
int check(){
	int minn_x=INT_MAX,minn_y=INT_MAX,maxn_x=-1,maxn_y=-1;
	for(int i=1;i<=n;i++) {
		minn_x=min(minn_x,a[i].x);
		minn_y=min(minn_y,a[i].y);
		maxn_x=max(maxn_x,a[i].x);
		maxn_y=max(maxn_y,a[i].y);
	}
	if((maxn_y-minn_y+1)*(maxn_x-minn_x+1)==n) return 1;
	return 0;
}
signed main(){
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y;
	if(n<=4000){
		sort(a+1,a+n+1,cmp); ans=0; tot=0;
		for(int i=1;i<=n;i++){
			if(a[i].x!=ans) ans=a[i].x,a[i].x=++tot;
			else a[i].x=tot;
		}
		sort(a+1,a+n+1,cmp2); ans=0; tot=0;
		for(int i=1;i<=n;i++){
			if(a[i].y!=ans) ans=a[i].y,a[i].y=++tot;
			else a[i].y=tot;
		} ans=tot=0;
		for(int i=1;i<=n;i++) mp[a[i].x][a[i].y]=i;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++) vis[j]=0;
			vis[i]=1; q.push(i);
			while(!q.empty()){
				int x=a[q.front()].x,y=a[q.front()].y; q.pop();
				if(mp[x-1][y]!=0 && vis[mp[x-1][y]]==0) vis[mp[x-1][y]]=vis[mp[x][y]]+1,q.push(mp[x-1][y]);
				if(mp[x+1][y]!=0 && vis[mp[x+1][y]]==0) vis[mp[x+1][y]]=vis[mp[x][y]]+1,q.push(mp[x+1][y]);
				if(mp[x][y-1]!=0 && vis[mp[x][y-1]]==0) vis[mp[x][y-1]]=vis[mp[x][y]]+1,q.push(mp[x][y-1]);
				if(mp[x][y+1]!=0 && vis[mp[x][y+1]]==0) vis[mp[x][y+1]]=vis[mp[x][y]]+1,q.push(mp[x][y+1]);
			}
			for(int j=i+1;j<=n;j++) ans+=vis[j]-1;
		}
		cout<<ans;
		return 0;
	}
	if(check()==1){
		int minn_x=INT_MAX,minn_y=INT_MAX,maxn_x=-1,maxn_y=-1;
		for(int i=1;i<=n;i++) {
			minn_x=min(minn_x,a[i].x);
			minn_y=min(minn_y,a[i].y);
			maxn_x=max(maxn_x,a[i].x);
			maxn_y=max(maxn_y,a[i].y);
		}
		for(int i=1;i<=n;i++){
			int sum=(a[i].x-minn_x+1)*(maxn_y-minn_y+1);
			ans+=sum*(a[i].x);
			sum=(maxn_x-a[i].x+1)*(maxn_y-minn_y+1);
			ans-=sum*(a[i].x);
			sum=(a[i].y-minn_y+1)*(maxn_x-minn_x+1);
			ans+=sum*(a[i].y);
			sum=(maxn_y-a[i].y+1)*(maxn_x-minn_x+1);
			ans-=sum*(a[i].y);
		}
		cout<<ans;
		return 0;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++) g[a[i].x]++;
	for(int i=1;i<=5e5;i++) g[i]+=g[i-1];
	for(int i=1;i<=n;i++){
		if(a[i].x!=0) ans+=a[i].x*g[a[i].x-1];
		ans-=a[i].x*(n-g[a[i].x]);
	}
	sort(a+1,a+n+1,cmp2);
	for(int i=1;i<=5e5;i++) g[i]=0;
	for(int i=1;i<=n;i++) g[a[i].y]++;
	for(int i=1;i<=5e5;i++) g[i]+=g[i-1];
	for(int i=1;i<=n;i++){
		if(a[i].y!=0) ans+=a[i].y*g[a[i].y-1];
		ans-=a[i].y*(n-g[a[i].y]);
	}
	cout<<ans;
	return 0;
}
/*

*/